//
//  CXPayTypeTableView.m

//

#import "CXPayTypeTableView.h"
#import "CXPayTypeTableCell.h"
static NSString *const PayTypeSingCell = @"CXPayTypeTableCell";

@interface CXPayTypeTableView()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeight;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

@end
@implementation CXPayTypeTableView

+(instancetype)loadPayTypeView
{
    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:self options:nil].firstObject;
}


-(void)awakeFromNib
{
    [super awakeFromNib];
    [_confirmButton setBackgroundColor:HNTMainColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;

    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CXPayTypeTableCell class]) bundle:nil] forCellReuseIdentifier:PayTypeSingCell];
}


-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    _tableViewHeight.constant = 49*_dataArray.count;
    [_tableView reloadData];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CXPayTypeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:PayTypeSingCell];
    if (cell == nil)
    {
        cell = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([cell class]) owner:self options:nil].firstObject;
    }
    if (_dataArray.count)
    {
        cell.model = _dataArray[indexPath.row];
    }
    cell.selectionStyle = 0;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 49;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CXPayTypeModel *selectModel = _dataArray[indexPath.row];
    for (CXPayTypeModel *subModel in _dataArray)
    {
        if ([subModel.pay_id isEqualToString:selectModel.pay_id])
        {
            subModel.isSelect = @"1";
        }
        else
        {
            subModel.isSelect = @"0";
        }
    }
    [tableView reloadData];
}

- (IBAction)confirmClick:(UIButton *)sender
{
    
    if(self.delegate &&[self.delegate respondsToSelector:@selector(view:WithIsConfirm:andModel:)])
    {
        if (sender.tag == 1)
        {
            BOOL isSelect = NO;
            
            for (int i = 0; i<_dataArray.count; i++)
            {
                CXPayTypeModel *model = _dataArray[i];
                if ([model.isSelect isEqualToString:@"1"])
                {
                    NSLog(@"model.name%@",model.name);
                    [self.delegate view:self WithIsConfirm:sender.tag andModel:model];
                    isSelect = YES;
                }
            }
            if (isSelect == NO)
            {
                [MBManager showBriefAlert:@"请选择支付方式"];
            }
        }
        else
        {
            [self.delegate view:self WithIsConfirm:sender.tag andModel:nil];
        }
    }
}



@end
